import json
import re
import urllib.request
import urllib.parse

def translateresult(content):
    try:
        url = "http://fanyi.youdao.com/translate?smartresult=dict"
        data = {}
        data["i"] = content
        data["client"] = "fanyideskweb"
        data["doctype"] = "json"
        data["version"] = "2.1"
        data["keyfrom"] = "fanyi.web"
        data["action"] = "FY_BY_REALTlME"
        data = urllib.parse.urlencode(data).encode('utf-8')
        response = urllib.request.urlopen(url, data)
        html = response.read().decode('utf-8')
        html = json.loads(html)
        translateresult = re.sub('(^\s+|\s+$)', '',
                                 re.sub('[^a-zA-Z\s]', '', html['translateResult'][0][0]['tgt'], count=0), count=0)
        translateresultsub = json.loads(
            re.sub('\s', '', json.dumps(re.findall('\s+(.)', translateresult)), count=0).upper());
        translateresultlength = len(re.findall('\s+', translateresult))
        for i in range(0, translateresultlength):
            translateresult = re.sub('\s+(.)', translateresultsub[i], translateresult, count=1);
        return translateresult
    except:
        return 'TranslationErrors'
